#include "stdafx.h"
#include "include\Core\Engine.h"

namespace Core
{

	LRESULT CALLBACK WndProc(HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam)
	  {
		int a = 0;
		switch(message)
		{
		case WM_KEYDOWN:
		  switch(wParam)
		  {
		  case VK_ESCAPE:
			PostQuitMessage(0);
			break;
		  }
		  break;

		case WM_DESTROY:
		  PostQuitMessage(0);
		  break;
		}

		return DefWindowProc(hwnd, message, wParam, lParam);
	  }  

	Engine::Engine()
	{
		// Nothing to do here

	}

	void Engine::Init(const EngineDescription& desc)
	{
		this->description = desc;
		// fenster erstellen
	}

	void Engine::Run()
	{

		   WNDCLASS wc;
    MSG msg; 

    int screenWidth;
    int screenHeight;
    int captionHeight;

    ZeroMemory(&wc, sizeof(wc));
    wc.cbClsExtra = 0;
    wc.cbWndExtra = 0;
    wc.hbrBackground = (HBRUSH) GetStockObject(BLACK_BRUSH);
    wc.hCursor = LoadCursor(NULL, IDC_ARROW);  
    wc.hIcon = NULL;
    wc.hInstance = (HINSTANCE) this->description.window.Handle;  
    wc.lpfnWndProc = WndProc;
    wc.lpszClassName = this->description.window.title;
    wc.lpszMenuName = NULL;
    wc.style = CS_HREDRAW | CS_VREDRAW;

    RegisterClass(&wc);

    screenWidth = GetSystemMetrics(SM_CXSCREEN);
    screenHeight = GetSystemMetrics(SM_CYSCREEN);
    captionHeight = GetSystemMetrics(SM_CYCAPTION);

    this->hwnd = CreateWindow(wc.lpszClassName,wc.lpszClassName, 
      WS_CAPTION | WS_SYSMENU | WS_MINIMIZEBOX,
      0, 0, this->description.graphics.width, this->description.graphics.height, NULL, NULL, 
     (HINSTANCE) this->description.window.Handle, NULL);

    ShowWindow(this->hwnd, SW_SHOWNORMAL);
    UpdateWindow(this->hwnd);

    ZeroMemory(&msg, sizeof(MSG));
    while(WM_QUIT != msg.message)
    {
      if(PeekMessage(&msg, NULL, 0, 0, PM_REMOVE))
      { 
        DispatchMessage(&msg);
      }
    }

	}

}